<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Hash;
use Session;

class AqController extends Controller
{

    //index进入安全中心首页
    public function index()
    {
    	return view('home.aq.index');
    }
    
     public function sendyzm($phone,$yzm){
        // http://mailsystem.taidu8.com/mailCenter/sendMessage/sendMessage.do?send_Mobile=18518491023&content=625143&platform=朝阳体育
        // $text = '此信息是朝阳体育网站发送的短信验证信息，验证码'.$yzm.'，如非本人操作请忽略';
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, "http://mailsystem.taidu8.com/mailCenter/sendMessage/sendMessage.do?send_Mobile=".$phone."&content=".$yzm."&platform=朝阳体育");
        // curl_setopt($ch, CURLOPT_URL, "http://mailsystem.taidu8.com/mailCenter/sendMessage/textMessage.do?send_Mobile=".$phone."&platform=朝阳体育局&text=".$text);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        //执行并获取HTML文档内容
        $output = curl_exec($ch);

        //释放curl句柄
        curl_close($ch);

        //打印获得的数据
        return $output;
    }
    //手机验证
    public function phone()
    {
    	return view('home.aq.phone');
    }

    //进入密保设置前填写当前用户密码
    public function question()
    {
    	return view('home.aq.question');
   	}

   	//验证密码是否正确
   	public function verify(Request $request)
   	{
   		//判断数据
      $data = $request -> except('_token');
   		// $data = DB::table('users') -> where('email',session['email']) -> first();
      if($data['password'] == 1)
      {
        return redirect('home/aq/answer');

      }else
      {
        return back() -> with(['info' => '密码不正确']);
      }

   	}

    //answer 设置密保问题页面
    public function answer()
    {
      return view('home.aq.answer');
    }

    //sure 将密保问题及答案存储
    public function sure(Request $request)
    {
      $data = $request -> except('_token');
      //获取用户id
      $data['uid'] = DB::table('users') -> where('email','1111qaz@qq.com') -> first() -> id;

      //判断获取的数据是否为空
      if($data['question1'] != '请选择')
      {
        //将数据添加到数据库
        $res = DB::table('safe') -> insert([
          'uid' => $data['uid'],
          'question' => $data['question1'],
          'answer' => $data['answer1'],
          ]);
      }
      if($data['question2'] != '请选择')
      {
        $r = DB::table('safe') -> insert([
        'uid' => $data['uid'],
        'question' => $data['question2'],
        'answer' => $data['answer2'],
        ]);
      }
      if($res || $r)
      {
        return redirect('/home/aq/index') -> with(['info' => '添加成功']);
      }else
      {
        return back() -> with(['info' => '请重新添加']);
      }
    }

    //自助解封登录界面
    public function login()
    {
      return view('home.aq.login');
    }

    //clear
    public function clear(Request $request)
    {
      //判断数据类型
      $this -> validate($request,[
        'email' => 'required|email',
        'password' => 'required',
        ],[
        'email.required' => '请输入账号',        
        'email.email' => '请正确输入邮箱账号',
        'password.required' => '请输入密码',        
        ]);
      $data = $request -> except('_token');
      //提取数据库数据
      $a = DB::table('users') -> where('email',$data['email']) -> first();
      if($a)
      {
        //判断账户是否被冻结
        if($a -> auth != 7)
        {
          return back() -> with(['info' => '您的账号未被冻结']);
        }

        //验证密码是否正确
        if (Hash::check($data['password'], $a -> password))
        {
          return view('/home/aq/clear',['data' => $a]);

        }else
        {
          return back() -> with(['info' => '密码错误']);
        }

      }else
      {
        return back() -> with(['info' => '您的账号不存在']);
      }
    }

     //验证手机号,验证码是否正确(judge)
     public function judge(Request $request)
     {
      $this -> validate($request,[
        'phone' => 'required',
        'code' => 'required',
        ],[
        'phone.required' => '请输入账号',
        'code.required' => '验证码不能为空',
        ]);

      //判断是否为手机号
      if(strlen($request -> phone) == "11") 
      { 
      //上面部分判断长度是不是11位 
       $n = preg_match_all("/13[123569]{1}\d{8}|15[1235689]\d{8}|188\d{8}/",$request -> phone,$array); 
           
           $data = $request -> except('_token');
          $r = DB::table('safe') -> where('uid',$request['id']) -> first();

          //判断账户是否绑定手机号
          if($r['phone'])
          {
            return back() -> with(['info' => '该账号未绑定手机号,请电联客服解封']);
          }

          if($data['phone'] == $r['phone'])
          {
            return redirect('/home/aq/index') -> with(['info' => '解封成功']);
          }else
          {
            return back() -> with(['info' => '手机号错误']);
          }
      }else
      { 
          return back() -> with(['info' => '手机号必须为11位']);
      } 
      
      
     }

}
